package com.chinasoft.carWithuserservice.mapper;


import com.chinasoft.ms.dto.CarWithUserDTO;
import com.chinasoft.ms.pojo.CarWithUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface CarWithUserMapper {


    @Select("<script>" +
            "SELECT * FROM car_with_user " +
            "<where>" +
            "   <if test='carId != null'>AND car_id = #{carId}</if>" +
            "   <if test='userId != null'>AND user_id = #{userId}</if>" +
            "</where>" +
            "LIMIT #{offset}, #{pageSize}" +
            "</script>")
    List<CarWithUser> selectPage(@Param("carId") Long carId,
                                 @Param("userId") Long userId,
                                 @Param("offset") Integer offset,
                                 @Param("pageSize") Integer pageSize);

    @Select("<script>" +
            "SELECT COUNT(*) FROM car_with_user " +
            "<where>" +
            "   <if test='carId != null'>AND car_id = #{carId}</if>" +
            "   <if test='userId != null'>AND user_id = #{userId}</if>" +
            "</where>" +
            "</script>")
    Integer selectCount(@Param("carId") Long carId,
                        @Param("userId") Long userId);

    @Insert("INSERT INTO car_with_user(car_id, user_id, rental_start, rental_end, extension_count) " +
            "VALUES(#{carId}, #{userId}, #{rentalStart}, #{rentalEnd}, #{extensionCount})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(CarWithUserDTO carWithUser);

    @Update("UPDATE car_with_user SET " +
            "car_id = #{carId}, " +
            "user_id = #{userId}, " +
            "rental_start = #{rentalStart}, " +
            "rental_end = #{rentalEnd}, " +
            "extension_count = #{extensionCount} " +
            "WHERE id = #{id}")
    int update(CarWithUserDTO carWithUser);

    @Delete("DELETE FROM car_with_user WHERE id = #{id}")
    int deleteById(Long id);

    @Delete("DELETE FROM car_with_user WHERE car_id = #{carId}")
    int deleteByCarId(Long carId);
}
